package sunyu.tools.activemq.receiver;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;
import java.io.Serializable;

@Component
public class QueueReceiver implements MessageListener {

    private static final Logger logger = LoggerFactory.getLogger(Thread.currentThread().getClass());

    @Override
    public void onMessage(Message message) {
        try {
            if (message instanceof TextMessage) {
                TextMessage msg = (TextMessage) message;
                logger.debug(msg.getText());
            } else if (message instanceof ObjectMessage) {
                ObjectMessage msg = (ObjectMessage) message;
                Serializable obj = msg.getObject();
                logger.debug(obj.toString());
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

}
